/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.envelopinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.envelopinfo.entity.EnvelopInfo;
import com.thinkgem.jeesite.modules.envelopinfo.service.EnvelopInfoService;

/**
 * 生成红包Controller
 * @author thinkgem
 * @version 2017-08-22
 */
@Controller
@RequestMapping(value = "${adminPath}/envelopinfo/envelopInfo")
public class EnvelopInfoController extends BaseController {

	@Autowired
	private EnvelopInfoService envelopInfoService;
	
	@ModelAttribute
	public EnvelopInfo get(@RequestParam(required=false) String id) {
		EnvelopInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = envelopInfoService.get(id);
		}
		if (entity == null){
			entity = new EnvelopInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("envelopinfo:envelopInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(EnvelopInfo envelopInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EnvelopInfo> page = envelopInfoService.findPage(new Page<EnvelopInfo>(request, response), envelopInfo); 
		model.addAttribute("page", page);
		return "modules/envelopinfo/envelopInfoList";
	}

	@RequiresPermissions("envelopinfo:envelopInfo:view")
	@RequestMapping(value = "form")
	public String form(EnvelopInfo envelopInfo, Model model) {
		model.addAttribute("envelopInfo", envelopInfo);
		return "modules/envelopinfo/envelopInfoForm";
	}

	@RequiresPermissions("envelopinfo:envelopInfo:edit")
	@RequestMapping(value = "save")
	public String save(EnvelopInfo envelopInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, envelopInfo)){
			return form(envelopInfo, model);
		}
		if (envelopInfo.getIsNewRecord()) {
			envelopInfo.setCreateTime(new java.util.Date());
		}
		envelopInfoService.save(envelopInfo);
		addMessage(redirectAttributes, "保存红包成功");
		return "redirect:"+Global.getAdminPath()+"/envelopinfo/envelopInfo/?orderBy=envelopSort asc,createTime desc";
	}
	
	@RequiresPermissions("envelopinfo:envelopInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(EnvelopInfo envelopInfo, RedirectAttributes redirectAttributes) {
		envelopInfoService.delete(envelopInfo);
		addMessage(redirectAttributes, "删除红包成功");
		return "redirect:"+Global.getAdminPath()+"/envelopinfo/envelopInfo/?orderBy=envelopSort asc,createTime desc";
	}

}